const Joi = require('joi');

class PriceValidate {

  // 价格列表参数校验
  async list(params) {

    const schema = Joi.object({
      product_id: Joi.string(),
      remark: Joi.string()
        .pattern(/^[^\s]+$/)
        .messages({
          'string.pattern': '描述不能包含特殊字符',
        }),
      type: Joi.string(),
      is_expires: Joi.number(),
      order: Joi.string()
        .default('create_time'),
      method: Joi.string()
        .default('desc'),
      pageIndex: Joi.string()
        .default('1'),
      pageSize: Joi.string()
        .default('10'),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 新增价格参数校验
  async insert(params) {

    const schema = Joi.object({
      product_id: Joi.string()
        .required()
        .messages({
          'string.empty': '产品id不能为空',
          'string.required': '产品id为必填项',
        }),

      price: Joi.string()
        .required({
          'number.empty': '价格不能为空',
          'number.required': '价格为必填项',
        }),

      type: Joi.string()
        .required()
        .messages({
          'string.empty': '类型不能为空',
          'string.required': '类型为必填项',
        }),

      remark: Joi.string()
        .pattern(/^[^\s]+$/)
        .messages({
          'string.pattern': '备注不能包含额数字符',
        }),

      end_time: Joi.string(),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 价格更新参数校验
  async update(params) {

    const schema = Joi.object({
      remark: Joi.string(),
      id: Joi.string()
        .required()
        .messages({
          'string.empty': 'id不能为空',
          'string.required': 'id为必填项',
        }),
    });

    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

  // 删除价格更新参数校验
  async delete(params) {

    const schema = Joi.object({
      ids: Joi.array(),
    });


    try {
      const value = await schema.validateAsync(params);
      return value;
    } catch (error) {
      throw error;
    }
  }

}

module.exports = new PriceValidate();
